﻿// Inner Fire 游戏引擎库
// 渲染项目类-基类
//
// Copyright (c) 2024 by 尔西大帝. All rights reserved.
//
// 所有的RenderItem类都需要继承此类
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-12-21

#include "dx_image.h"
#include "dx_text.h"

namespace ifire::dx {

DxImage::DxImage(const DxImageDesc& desc)
    : UIRenderItem(desc.Widget, 1), mElement(desc.pElement) {}

void DxImage::Update(XMFLOAT2 client_size, UIInstancedData& data) {
  // 如果项目不可见，直接设置空
  if (mVisible == false || mElement == nullptr) {
    data.SetEmpty();
    return;
  }

  const float texture_size = mElement->parent->texture_size;
  const RectF& rect = mElement->First()->rectf;
  data.uv_offset.x = rect.left / texture_size;
  data.uv_offset.y = rect.top / texture_size;
  data.uv_scale.x = (rect.right - rect.left) / texture_size;
  data.uv_scale.y = (rect.bottom - rect.top) / texture_size;

  // 计算Widget
  auto widget = CalcWidget();

  data.offset.x = (widget.position.x / client_size.x) * 2.0f - 1.0f;
  data.offset.y = 1.0f - (widget.position.y / client_size.y) * 2.0f;
  data.scale.x = widget.size.x / client_size.x;
  data.scale.y = widget.size.y / client_size.y;

  data.scale.x *= widget.scale.x;
  data.scale.y *= widget.scale.y;

  data.zindex = widget.GetZ();
}

void DxImage::SetPosition(XMFLOAT2 position) { mWidget.position = position; }

} // namespace ifire::dx